﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using WebApplication8.Data;

namespace Server.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class ClassesController : ControllerBase
    {
        private readonly ApplicationDbContext _context;

        public ClassesController(ApplicationDbContext context)
        {
            _context = context;
        }

        // GET: api/Classes
        [HttpGet]
        public async Task<ActionResult<IEnumerable<BanJi>>> GetBanJi()
        {
            return await _context.BanJi.ToListAsync();
        }

        // GET: api/Classes/5
        [HttpGet("{id}")]
        public async Task<ActionResult<BanJi>> GetBanJi(string id)
        {
            var banJi = await _context.BanJi.FindAsync(id);

            if (banJi == null)
            {
                return NotFound();
            }

            return banJi;
        }

        // PUT: api/Classes/5
        [HttpPut("{id}")]
        public async Task<IActionResult> PutBanJi(string id, BanJi banJi)
        {
            if (id != banJi.Id)
            {
                return BadRequest();
            }

            _context.Entry(banJi).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!BanJiExists(id))
                {
                    return NotFound();
                }
                else
                {
                    throw;
                }
            }

            return NoContent();
        }

        // POST: api/Classes
        [HttpPost]
        public async Task<ActionResult<BanJi>> PostBanJi(BanJi banJi)
        {
            _context.BanJi.Add(banJi);
            await _context.SaveChangesAsync();

            return CreatedAtAction("GetBanJi", new { id = banJi.Id }, banJi);
        }

        // DELETE: api/Classes/5
        [HttpDelete("{id}")]
        public async Task<ActionResult<BanJi>> DeleteBanJi(string id)
        {
            var banJi = await _context.BanJi.FindAsync(id);
            if (banJi == null)
            {
                return NotFound();
            }

            _context.BanJi.Remove(banJi);
            await _context.SaveChangesAsync();

            return banJi;
        }

        private bool BanJiExists(string id)
        {
            return _context.BanJi.Any(e => e.Id == id);
        }
    }
}
